package Utils;

import info.monitorenter.cpdetector.io.*;

import java.io.File;

/**
 * 根据文件绝对路径判断文件的编码
 */
public class GetFileEncodingByPath {

	public static String getEncodingByPath(String filePath) {
		String fileEncoding=null;
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		detector.add(new ParsingDetector(false));
		detector.add(JChardetFacade.getInstance());
		detector.add(ASCIIDetector.getInstance());
		detector.add(UnicodeDetector.getInstance());
		 java.nio.charset.Charset charset = null;
		 File f = new File(filePath);
		 try {
			 charset = detector.detectCodepage(f.toURI().toURL());
			 if (charset != null){
				 fileEncoding=charset.name();
			 }else{
				 fileEncoding=null;
			 }
		 } catch (Exception ex) {
			 ex.printStackTrace();
		 }
		return fileEncoding;
	}
}
